package spring;

import java.util.concurrent.atomic.AtomicInteger;

public class Test {
	
	private static final int COUNT_BITS = Integer.SIZE - 3;
	private static final int CAPACITY   = (1 << COUNT_BITS) - 1;
	private static final int RUNNING    = -1 << COUNT_BITS;
	private static final int SHUTDOWN   =  0 << COUNT_BITS;
	private static final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
	
	private static int ctlOf(int rs, int wc) { return rs | wc; }
	
	private static int workerCountOf(int c)  { return c & CAPACITY; }
	
	private static int runStateOf(int c)     { return c & ~CAPACITY; }
	
	private static boolean isRunning(int c) {
        return c < SHUTDOWN;
    }
	
	private static boolean compareAndIncrementWorkerCount(int expect) {
        return ctl.compareAndSet(expect, expect + 1);
    }
	
	public static void main(String[] args){
		System.out.println(ctl.get());
		System.out.println(workerCountOf(ctl.get()));
		System.out.println(isRunning(ctl.get()));
		System.out.println(runStateOf(ctl.get()));
		System.out.println("SHUTDOWN:"+SHUTDOWN);
		System.out.println(workerCountOf(ctl.get()));
		boolean result = compareAndIncrementWorkerCount(ctl.get());
		System.out.println(result);
		System.out.println(ctl.get());
		System.out.println(runStateOf(ctl.get()));
		System.out.println(workerCountOf(ctl.get()));
	}
	
	

}
